<template>
  <div class="commonheader">
    <ul>
      <li><router-link to="/" class="sy">首页</router-link></li>
      <li v-for="(item, key) in catelist" :key="item.id">
        <a
          class="a"
          href="javascript:;"
          @click="setColor(key, item.id)"
          :class="{ cateactive: key == $store.state.category.nowcateindex }"
        >
          {{ item.name }}
        </a>
        <div class="layer">
          <ul>
            <li v-for="it in item.children" :key="it.id">
              <router-link :to="'/sub?id=' + it.id">
                <img :src="it.picture" alt="" />
                <p>{{ it.name }}</p>
              </router-link>
            </li>
          </ul>
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  computed: {
    catelist() {
      return this.$store.state.category.cate;
    },
  },
  methods: {
    setColor(k, id) {
      //将k保存到vuex中
      this.$store.commit("category/setCateIndex", k);
      //条状
      this.$router.push("/cate?id=" + id);
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/css/color.scss";
.commonheader {
  width: 100%;
  height: 100%;
  .sy {
    font-size: 16px;
    line-height: 32px;
    height: 32px;
    display: inline-block;
  }
  ul {
    width: 100%;
    height: 100%;
    display: flex;
    padding-left: 40px;
    align-items: center;
    li {
      margin-right: 40px;
      width: 38px;
      text-align: center;
      color: #333;
      .a {
        font-size: 16px;
        line-height: 32px;
        height: 32px;
        display: inline-block;
      }
      &:hover {
        > a {
          color: $sucColor;
          border-bottom: 1px solid $sucColor;
        }
        > .layer {
          height: 132px;
          opacity: 1;
        }
      }
    }
  }
  .layer {
    z-index: 999;
    width: 1240px;
    background: #fff;
    height: 0px;
    overflow: hidden;
    opacity: 0;
    box-shadow: 0 0 5px #ccc;
    transition: all 0.3s 0.2s;
    position: absolute;
    left: 0px;
    top: 110px;
    ul {
      display: flex;
      align-items: center;
      justify-content: flex-start;
      padding: 0 70px;
      height: 132px;
      li {
        width: 110px;
        text-align: center;
        img {
          width: 60%;
        }
        p {
          font-size: 16px;
        }
      }
    }
  }
}
.cateactive {
  color: $sucColor;
  border-bottom: 1px solid $sucColor;
}
</style>